package cn.com.haopy.yue.ai.admin.web;


import cn.com.haopy.yue.ai.admin.annotation.RequiresPermissionsDesc;
import cn.com.haopy.yue.ai.core.config.storage.StorageService;
import cn.com.haopy.yue.ai.core.utils.ResponseUtil;
import cn.com.haopy.yue.ai.core.validator.Order;
import cn.com.haopy.yue.ai.core.validator.Sort;
import cn.com.haopy.yue.ai.db.entity.arisk.HOSStorage;
import cn.com.haopy.yue.ai.db.service.hos.HOSStorageService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.io.IOException;

@RestController
@RequestMapping("/admin/storage")
@Validated
@Api(tags = "对象存储管理相关")
public class AdminStorageController {
    private final Log logger = LogFactory.getLog(AdminStorageController.class);

    @Autowired
    private StorageService storageService;
    @Autowired
    private HOSStorageService busStorageService;

    @RequiresPermissions("admin:storage:list")
    @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"}, button = "查询")
    @GetMapping("/list")
    @ApiOperation("查询")
    public Object list(String key, String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "create_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        Page<HOSStorage> storageList = busStorageService.querySelective(key, name, page, limit, sort, order);
        return ResponseUtil.okPage(storageList);
    }

    @RequiresPermissions("admin:storage:create")
    @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"}, button = "上传")
    @PostMapping("/create")
    @ApiOperation("上传")
    public Object create(@RequestParam("file") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        boolean checkFileFormat = storageService.checkFormats(originalFilename);
        if (!checkFileFormat) {
            return ResponseUtil.fail("不支持的文件格式");
        }

        HOSStorage storage = storageService.store(file.getInputStream(), file.getSize(),
                file.getContentType(), originalFilename);
        return ResponseUtil.ok(storage);
    }

    @RequiresPermissions("admin:storage:read")
    @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"}, button = "详情")
    @PostMapping("/read")
    @ApiOperation("详情")
    public Object read(@NotNull Integer id) {
        HOSStorage storageInfo = busStorageService.getById(id);
        if (storageInfo == null) {
            return ResponseUtil.badArgumentValue();
        }
        return ResponseUtil.ok(storageInfo);
    }

    @RequiresPermissions("admin:storage:update")
    @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"}, button = "编辑")
    @PostMapping("/update")
    @ApiOperation("编辑")
    public Object update(@RequestBody HOSStorage storage) {
        if (!busStorageService.updateById(storage)) {
            return ResponseUtil.updatedDataFailed();
        }
        return ResponseUtil.ok(storage);
    }

    @RequiresPermissions("admin:storage:delete")
    @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"}, button = "删除")
    @PostMapping("/delete")
    @ApiOperation("删除")
    public Object delete(@RequestBody HOSStorage storage) {
        String key = storage.getKey();
        if (!StringUtils.hasText(key)) {
            return ResponseUtil.badArgument();
        }
        busStorageService.deleteByKey(key);
        storageService.delete(key);
        return ResponseUtil.ok();
    }
}
